package gui;

import gui.houseeditor.HouseModel;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

import util.Casella;
import attori.sensori.Sensore;
import attori.sensori.SensorePorta;
import attori.sensori.SensoreVolume;

@SuppressWarnings("serial")
public class DialogSensorePorta extends JDialog {

	JPanel p = new JPanel();
	JPanel p1 = new JPanel();
	HouseModel model;
	ButtonGroup group;
	List<Sensore> sensori;
	Casella casella;

	public DialogSensorePorta(Frame f, HouseModel model, Casella c) {
		super(f);
		this.model = model;
		casella = c;
		setTitle("Sensore porta");
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(
				"img/icona_porta.png"));
		setResizable(false);
		setModal(true);
		this.setLayout(new BorderLayout());
		p.setLayout(new GridBagLayout());
		init();
		initButtons();
		add(p, BorderLayout.CENTER);
		add(p1, BorderLayout.SOUTH);
		p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));
		pack();
		this.setLocationRelativeTo(f);
		setVisible(true);
	}

	private void initButtons() {
		JButton ok = new JButton("OK");
		ok.setAction(new OKAction());
		JButton annulla = new JButton("Annulla");
		annulla.setAction(new AnnullaAction());
		p1.add(ok);
		this.getRootPane().setDefaultButton(ok);
		p1.add(annulla);
	}

	private class AnnullaAction extends AbstractAction {

		public AnnullaAction() {
			super("Annulla");
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke("control D"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			DialogSensorePorta.this.dispose();
		}

	}

	private class OKAction extends AbstractAction {

		public OKAction() {
			super("OK");
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke("control D"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {

			for (Sensore sensore : sensori) {
				Casella[][] areaSensore = sensore.getArea();
				for (int i = 0; i < areaSensore.length; i++) {
					for (int j = 0; j < areaSensore[0].length; j++) {
						if (areaSensore[i][j].equals(casella)) {
							if (group.getElements().nextElement().isSelected()) {
								sensore.send(new SensorePorta.Accendi());
							} else {
								sensore.send(new SensorePorta.Spegni());
							}
						}

					}
				}

			}
			dispose();
		}
	}

	private void init() {

		sensori = model.getSensoriPorta();
		JLabel name = new JLabel("", SwingConstants.CENTER);
		p.add(name, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		group = new ButtonGroup();
		JRadioButton ON = new JRadioButton("ON");
		JRadioButton OFF = new JRadioButton("OFF");
		group.add(ON);
		group.add(OFF);
		for (Sensore sensore : sensori) {
			Casella[][] areaSensore = sensore.getArea();
			for (int i = 0; i < areaSensore.length; i++) {
				for (int j = 0; j < areaSensore[0].length; j++) {
					if (areaSensore[i][j].equals(casella)) {
						if (sensore.currentStatus() == SensoreVolume.OPERATIVE) {
							group.setSelected(ON.getModel(), true);
						} else {
							group.setSelected(OFF.getModel(), true);
						}
					}

				}
			}

		}

		p.add(ON, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));
		p.add(OFF, new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));

	}
}
